
import baseUrl from '../api/getUrl'
import axios from 'axios';

export default async(url = '', data = {}, type = 'GET', method = 'fetch') => {
  type = type.toUpperCase();
  url = baseUrl.baseUrl + url;
  const token = window.localStorage.get("token");

  if (type == 'GET'){
    // return axios.get(url,data);
    return axios(url, {
      method: 'GET',
      params : data,
      mode: 'no-cors',
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json',
        'token' : token,
      },
      withCredentials: true,
      credentials: 'same-origin',
    })
  }

  if (type == 'POST'){
    // return axios.get(url,data);
    return axios(url, {
      method: 'POST',
      params : data,
      mode: 'no-cors',
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json',
        'token' : token,
      },
      withCredentials: true,
      credentials: 'same-origin',
    })
  }
  if (type == 'DELETE'){
      // return axios.get(url,data);
      return axios(url,{
          method: 'DELETE',
          mode: 'no-cors',
          headers: {
              'Access-Control-Allow-Origin': '*',
              'Content-Type': 'application/json',
              'token' : token,
          },
          withCredentials: true,
          credentials: 'same-origin',
      })
  }
    if (type == 'PUT'){
        // return axios.get(url,data);
        return axios(url, {
            method: 'PUT',
            params : data,
            mode: 'no-cors',
            headers: {
                'Access-Control-Allow-Origin': '*',
                'Content-Type': 'application/json',
                'token' : token,
            },
            withCredentials: true,
            credentials: 'same-origin',
        })
    }


    // if (type == 'GET') {
  //   let dataStr = ''; //数据拼接字符串
  //   Object.keys(data).forEach(key => {
  //     dataStr += key + '=' + data[key] + '&';
  //   })
  //
  //   if (dataStr !== '') {
  //     dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
  //     url = url + '?' + dataStr;
  //   }
  // }
  //
  // if (type == 'REST'){
  //   url = url + data;
  //   console.log(url)
  // }
  //
  // if (window.fetch && method == 'fetch') {
  //   let requestConfig = {
  //     credentials: 'include',
  //     method: type,
  //     headers: {
  //       // 'Access-Control-Allow-Origin' : '*',
  //       'Accept': 'application/json',
  //       'Content-Type': 'application/json'
  //     },
  //     // mode: "cors",
  //     // cache: "force-cache"
  //   }
  //
  //   if (type == 'POST') {
  //     Object.defineProperty(requestConfig, 'body', {
  //       value: JSON.stringify(data)
  //     })
  //   }
  //
  //   try {
  //     const response = await fetch(url, requestConfig);
  //     const responseJson = await response.json();
  //     return responseJson
  //   } catch (error) {
  //     throw new Error(error)
  //   }
  // } else {
  //   return new Promise((resolve, reject) => {
  //     let requestObj;
  //     if (window.XMLHttpRequest) {
  //       requestObj = new XMLHttpRequest();
  //     } else {
  //       requestObj = new ActiveXObject;
  //     }
  //
  //     let sendData = '';
  //     if (type == 'POST') {
  //       sendData = JSON.stringify(data);
  //     }
  //
  //     requestObj.open(type, url, true);
  //     requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  //     requestObj.send(sendData);
  //
  //     requestObj.onreadystatechange = () => {
  //       if (requestObj.readyState == 4) {
  //         if (requestObj.status == 200) {
  //           let obj = requestObj.response
  //           if (typeof obj !== 'object') {
  //             obj = JSON.parse(obj);
  //           }
  //           resolve(obj)
  //         } else {
  //           reject(requestObj)
  //         }
  //       }
  //     }
  //   })
  // }
}
